#region License

// This file is part of Grawlix.
// 
// Grawlix is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// Grawlix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public
// License along with Grawlix.  If not, see
// <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Grawlix.Services
{
	/// <summary>
	/// A serialization object reference that results in a null
	/// object.
	/// </summary>
	[Serializable]
	public class GameKernelObjectReference : IObjectReference,
	                                         ISerializable
	{
		/// <summary>
		/// Initializes the reference.
		/// </summary>
		/// <param name="info">The <see cref="SerializationInfo"/>
		/// available to use.</param>
		/// <param name="context">The <see cref="StreamingContext"/> of
		/// the serialization.</param>
		/// <exception cref="ArgumentNullException"><c>info</c> is
		/// null.</exception>
		[SecurityPermission(SecurityAction.LinkDemand,
			Flags = SecurityPermissionFlag.SerializationFormatter)]
		protected GameKernelObjectReference(
			SerializationInfo info, StreamingContext context)
		{
			if (info == null)
				throw new ArgumentNullException("info");
		}

		/// <summary>
		/// Populates a 
		/// <see cref="T:System.Runtime.Serialization.SerializationInfo"/>
		/// with the data needed to serialize the target object.
		/// </summary>
		/// <param name="info">The 
		/// <see cref="T:System.Runtime.Serialization.SerializationInfo"/>
		/// to populate with data.</param>
		/// <param name="context">The destination (see 
		/// <see cref="T:System.Runtime.Serialization.StreamingContext"/>
		/// ) for this serialization.</param>
		/// <exception cref="T:System.Security.SecurityException">
		/// The caller does not have the required permission.
		/// </exception>
		/// <exception cref="ArgumentNullException"><c>info</c> is
		/// null.</exception>
		[SecurityPermission(SecurityAction.LinkDemand,
			Flags = SecurityPermissionFlag.SerializationFormatter)]
		public virtual void GetObjectData(SerializationInfo info,
		                                  StreamingContext context)
		{
			if (info == null)
				throw new ArgumentNullException("info");
		}

		/// <summary>
		/// Returns the real object that should be deserialized, rather
		/// than the object that the serialized stream specifies.
		/// </summary>
		/// <param name="context">The 
		/// <see cref="T:System.Runtime.Serialization.StreamingContext"/>
		/// from which the current object is deserialized.</param>
		/// <returns>
		/// Returns the actual object that is put into the graph.
		/// </returns>
		/// <exception cref="T:System.Security.SecurityException">
		/// The caller does not have the required permission. The call
		/// will not work on a medium trusted server.
		/// </exception>
		/// <exception cref="SerializationException">Unable to
		/// deserialize persistent data that relies on a game kernel
		/// without access to the game kernel.</exception>
		[SecurityPermission(SecurityAction.LinkDemand,
			Flags = SecurityPermissionFlag.SerializationFormatter)]
		public virtual object GetRealObject(
			StreamingContext context)
		{
			return
				GameKernelStreamingContext.GetContextKernel(context);
		}
	}
}